package com.wsc.wisdomchina.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.ACache;
import com.wsc.wisdomchina.Tools.JsonTools;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.activitity.wisdomParty.PartyStartTestActivity;
import com.wsc.wisdomchina.adapter.PartyLineTestAdapter;
import com.wsc.wisdomchina.application.MyApplication;
import com.wsc.wisdomchina.callback.ContributorView;
import com.wsc.wisdomchina.callback.ShowChatViewInterface;
import com.wsc.wisdomchina.javaBean.ApplyListBean;
import com.wsc.wisdomchina.javaBean.RespObj;
import com.wsc.wisdomchina.javaBean.wisdomParty.PartyLineTestBean;
import com.wsc.wisdomchina.presenter.ContributorPresenter;

import java.util.List;

/**
 * 在线答题Fragment
 */
public class PartyLineTestFragment extends Fragment implements ContributorView {
    private PullToRefreshListView fragment_listView;//列表
    private ListView actualListView;
    private final String PartyFragmentString = PartyLineTestFragment.class.getSimpleName();

    private String cid;
    private String pId;
    private String name;
    private Context mcontext;
    private int skip = 0;
    private final int limit = 15;
    private ContributorPresenter contributorPresenter;
    private List<PartyLineTestBean> lineTestList;
    private PartyLineTestAdapter adapter;
    private boolean isfresh = true;
    private MyApplication myApplication;
    private ACache aCache;
    private ShowChatViewInterface mCallBack;// 接口变量

    public static PartyLineTestFragment newInstance(Bundle b) {
        PartyLineTestFragment selectedOneFragment = new PartyLineTestFragment();
        selectedOneFragment.setArguments(b);
        return selectedOneFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        cid = getArguments().getString("cid");
        pId = getArguments().getString("pId");
        name = getArguments().getString("name");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment_party_line_test, container, false);
        mcontext = getActivity();
        aCache = ACache.get(mcontext);
        myApplication = (MyApplication) getActivity().getApplication();
        contributorPresenter = new ContributorPresenter(this, mcontext);
        initView(view);
        showData();
        return view;
    }

    /**
     * @param view
     * @Description 初始化View控件
     * @author jaily.zhang
     */
    private void initView(View view) {

        fragment_listView = ((PullToRefreshListView) view.findViewById(R.id.fragment_listView));
        fragment_listView.setMode(PullToRefreshBase.Mode.BOTH);
        actualListView = fragment_listView.getRefreshableView();

        actualListView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        actualListView.setDivider(null);
        Tools.showRefreshView(fragment_listView);

        actualListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if (lineTestList != null && lineTestList.size() > 0) {
                    Intent intent = new Intent(mcontext, PartyStartTestActivity.class);
                    intent.putExtra("pmcId", lineTestList.get(position - 1).getId());
                    intent.putExtra("memId", pId);
                    intent.putExtra("streetId",cid);
                    startActivity(intent);
                }
            }
        });


        fragment_listView.setOnRefreshListener(new OnRefreshListener<ListView>() {

            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                // TODO Auto-generated method stub
                // Update the LastUpdatedLabe
                // 处理下拉刷新的业务
                if (refreshView.getCurrentMode().equals(PullToRefreshBase.Mode.PULL_FROM_START)) {
                    isfresh = true;
                    Tools.showRefreshView(fragment_listView);
                    skip = 0;
                    showData();
                } else {
                    isfresh = false;
                    Tools.showRefreshView(fragment_listView);
                    skip += limit;
                    showData();
                }

            }
        });
    }

    /**
     * @Description 服务器获取数据
     * @author jaily.zhang
     */
    private void showData() {
        contributorPresenter.getOnlineExamList(cid, skip, limit, pId, PartyFragmentString);
    }


    @Override
    public void onLoadContributorStart(String flag) {

    }


    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            if (flag.equals(PartyFragmentString)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    List<PartyLineTestBean> lineTest = JsonTools.jsonStringToList(jsonMessage, PartyLineTestBean.class);
                    if (lineTest != null && lineTest.size() > 0) {
                        if (isfresh) {
                            lineTestList = lineTest;
                            adapter = new PartyLineTestAdapter(mcontext, lineTest);
                            actualListView.setAdapter(adapter);
                        } else {
                            lineTestList.addAll(lineTest);
                            adapter.notifyDataSetChanged();
                        }

                    } else {
                        aCache.clear();
                    }

                }
            }
        } catch (Exception e) {

        }
    }


    @Override
    public void onLoadContributorComplete(String flag) {
        fragment_listView.onRefreshComplete();
    }

    @Override
    public void onLoadContributorError(String error, String flag) {
    }

    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }


    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        mCallBack.isShowChatView(false);
        if (myApplication.getInformationFragmentSelected() != null) {
            if (myApplication.getInformationFragmentSelected().equals(PartyFragmentString + name)) {
                isfresh = true;
                skip = 0;
            }
        }
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mCallBack = (ShowChatViewInterface) context;
    }

}
